package bard.partnermatchingsystem.controller;


import bard.partnermatchingsystem.Common.ErrorCode;
import bard.partnermatchingsystem.domain.R.R;
import bard.partnermatchingsystem.domain.dto.*;
import bard.partnermatchingsystem.domain.po.Team;
import bard.partnermatchingsystem.domain.po.User;
import bard.partnermatchingsystem.domain.po.UserTeam;
import bard.partnermatchingsystem.domain.vo.UserTeamVO;
import bard.partnermatchingsystem.exception.CommonException;
import bard.partnermatchingsystem.service.TeamService;
import bard.partnermatchingsystem.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/team")
@Slf4j
public class TeamController {
    @Resource
    private UserService userService;
    @Resource
    private TeamService teamService;
    @PostMapping("/add")
    public R<Long> addTeam(@RequestBody TeamAddDTO teamAddDTO, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (teamAddDTO == null) {
         throw new CommonException(ErrorCode.PARAMS_ERROR);
        }
        Long TeamId= teamService.addTeam(teamAddDTO, loginUser);
        return R.ok(TeamId);
    }
    @PostMapping("/delete")
    public R<Boolean> deleteTeam(@RequestParam Long teamId, HttpServletRequest request) {
        if (teamId == null||teamId<=0) {
            throw new CommonException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean remove = teamService.removeTeam(teamId,loginUser);
        if (!remove) {
            throw new CommonException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return R.ok(true);
    }

    @PostMapping("/update")
    public R<Boolean> updateTeam(@RequestBody TeamUpdateamDTO teamUpdateamDTO,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (teamUpdateamDTO == null) {
            throw new CommonException(ErrorCode.PARAMS_ERROR);
        }
        boolean update = teamService.updateTeam(teamUpdateamDTO,loginUser);
        if (!update) {
            throw new CommonException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }
        return R.ok(true);
    }
    @PostMapping("/get")
    public R<Team> getTeam(Team team) {
        if (team == null) {
            throw new CommonException(ErrorCode.PARAMS_ERROR);
        }
        Team team1 = teamService.getById(team.getId());
        if (team1 == null) {
            throw new CommonException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }
        return R.ok(team1);
    }
    @GetMapping("/list")
    public R<List<UserTeamVO>> listTeams(@RequestParam String searchText,
                                         @RequestParam(defaultValue = "0") int status,
                                         HttpServletRequest request) {
        boolean isAdmin = userService.isAdmin(request);
        User loginUser = userService.getLoginUser(request);
        List<UserTeamVO> userTeamVOS=  teamService.listTeams(searchText,status, isAdmin,loginUser);
        return R.ok(userTeamVOS);
    }
    @PostMapping("/join")
    public R<Boolean> joinTeam(@RequestBody TeamJoinDTO teamJoinDTO, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return R.ok(teamService.joinTeam(teamJoinDTO, loginUser));
    }
    @PostMapping("/quit")
    public R<Boolean> quitTeam(@RequestBody TeamQuitDTO teamquitDTO, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return R.ok(teamService.quitTeam(teamquitDTO, loginUser));
    }
    @GetMapping("/list/my/create")
    public R<List<UserTeamVO>> listMyCreateTeams(@RequestParam String searchText,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        List<UserTeamVO> userTeamVOS=  teamService.getMycreateTeams(searchText, loginUser);
        return R.ok(userTeamVOS);
    }
    @GetMapping("/list/my/join")
    public R<List<UserTeamVO>> listMyJoinTeams(@RequestParam String searchText,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        List<UserTeamVO> userTeamVOS=  teamService.getMyjoinTeams(searchText, loginUser);
        return R.ok(userTeamVOS);
    }


}
